package javaweb.selectcclass.test.controller;

import javaweb.selectcclass.test.entity.Course;
import javaweb.selectcclass.test.entity.Submit;
import javaweb.selectcclass.test.entity.User;
import javaweb.selectcclass.test.repo.CourseRepo;
import javaweb.selectcclass.test.repo.UserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;
import java.util.List;

@Controller
public class HomeController {

    @Autowired
    UserRepo userRepo;
    @Autowired
    CourseRepo courseRepo;

    @GetMapping("/")
    public String index(Authentication authentication){
        if(authentication != null){
            return "redirect:/home";
        }
        return "index";
    }

    @GetMapping("/login")
    public String toLogin(Authentication authentication){
        if(authentication != null) {
            return "redirect:/home";
        }
        return "login";
    }
    @GetMapping("/register")
    public String toRegister(Authentication authentication) {
        if(authentication != null) {
            return "redirect:/home";
        }
        return "register";
    }

    @PostMapping("/register")
    public String register(User user) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPwd(encoder.encode(user.getPwd()));
        if(userRepo.findByStuId(user.getStuId())!=null) {
            return "redirect:/register?err";
        }
        userRepo.save(user);
        return "redirect:/login";
    }

    @GetMapping("/home")
    public String home(Model model, Authentication authentication){
        String id = authentication.getName();
        User user = userRepo.findByStuId(id);
        model.addAttribute("user", user);
        return "home";
    }

    @GetMapping("/select")
    public String select(Model model, Authentication authentication) {
        String id = authentication.getName();
        User user = userRepo.findByStuId(id);
        model.addAttribute("user", user);

        List<Course> list = courseRepo.findAll();
        model.addAttribute("courses", list);
        return "select";
    }

    @PostMapping("/send")
    public String send(@Valid Submit submit, Errors errors, Model model, Authentication authentication) {
        String id = authentication.getName();
        User user = userRepo.findByStuId(id);
        if(errors.hasErrors()) {
            return "redirect:/select?err";
        }
        model.addAttribute("user", user);
        List<String> stringList = submit.getCourses();
        user.getCourses().clear();
        for (String courseId:stringList) {
            user.getCourses().add(new Course(courseId, null, null));
        }
        userRepo.save(user);
        user = userRepo.findByStuId(id);
        model.addAttribute("course", user.getCourses());
        return "send";
    }

}
